Gnc.EmployeeListBox = function () {
    Gnc.EmployeeListBox.superclass.constructor.call(this);
};
mini.extend(Gnc.EmployeeListBox, mini.Box, {
    uiCls: "gnc-employeelistbox",
    listUrl:bootPATH+'Js/Common/Employee/EmployeeListBoxView.htm',    
    DFC:'C439E411-4291-4E40-9591-21A0FCBC73A7',
    MPM:'5F10FE4D-D2E7-44D1-8AF3-C41E3E95A809',
    secretCheck:false,
    listbox:null,  
    showTaskType: false,
    showUnit:true,
    showUnitValue:true,  
    _create: function () {
        Gnc.EmployeeListBox.superclass._create.call(this);
        var html = mini.getRemoteText(this.listUrl);
        this.setControls([html]);        

        this.fieldName = mini.getbyName("listbox-fieldName",this);
        this.isMaster=this.fieldName.getText().indexOf('主管')>=0?true:false;
        //人员的选择
        this.employeeSelect = mini.getbyName("employeeSelect",this);
        this.employeeTypes=mini.getbyName("employeeTypes",this);
        //流程类型
        this.taskTypeSelect = mini.getbyName('taskTypeSelect',this);
        this.taskTypeSelect.hide();
        //比例
        this.unit=mini.getbyName("unit",this); 

        //listbox        
        this.listbox = mini.getbyName("employee-listbox",this);
        this.listbox.on('itemclick',function(record){        	
            var name = record.item.name;
            name = name.replace('(DFC)','').replace('(MPM)','');
            var n=name;
            var unitValue=name.indexOf('%')>=0?name.substring(name.indexOf('[')+1,name.length-1).replace('%',''):0;
            name=name.indexOf('%')>=0?name.substring(0,name.indexOf('[')):name;
            
            if(this.showTaskType){
                var type=record.item.name;
                type=type.replace(n,'').replace('(','').replace(')','');
                var typeId=type=='DFC'?this.DFC:this.MPM;
                this.taskTypeSelect.setNameValue(type,typeId);
            }
            if(this.showUnit)
                this.unit.setValue(unitValue);
            if(name.indexOf(">")>=0){
                var p=name.split(">");
                if(p.length>1){
                    var n=p[1];
                    name=n.substring(0,n.indexOf("<"));
                }
            }
            if(mini.isEmpty(this.employeeSelect.getValue()))
                this.employeeSelect.setNameValue(name,record.item.id);
            
            this.emName=name;
            this.emId=record.item.id;
            
            this.taskTypeId=this.taskTypeSelect.getValue();
            this.taskTypeName=this.taskTypeSelect.getText();
        	this.fire('employeeSelected',this.selectedItem);

        },this);

        //添加按钮
        var btn = mini.getbyName("btn_addemployee",this);
        btn.on('click',function(){
            this.onAdd();
        },this);
        //编辑按钮
        var btn = mini.getbyName("btn_editemployee",this);
        btn.on('click',function(){
            this.onEdit();
        },this);
        //删除按钮
        btn = mini.getbyName("btn_removeemployee",this);
        btn.on('click',function(){
        	this.onDelete();
        },this);

        this.employeeSelect.on('employeeAutoSelected',function(obj){
            this.account=obj.loginAccount;      
        },this);

    },
	//添加
    onAdd:function() {
        var me = this;
        var v="0";
        if(this.secretCheck){
            v=GetUserSecret(this.account);
            if(v=="0"){
                mini.alert('账号：'+this.account+"密级低于项目密级！", '警告');
                return;
            }
        }
        var name = me.employeeSelect.getText();
        var unit_value=me.unit.getValue();
        if(name.indexOf('_')>-1)
            name=name.substring(0,name.indexOf('_'));
        if(me.showUnitValue){
            name=name+'['+unit_value+'%]';
        }
        var value = me.employeeSelect.getValue();
        if(mini.isEmpty(value)) {
            return;
        }
        var list=me.listbox.getData();
        for(var i=0;i<list.length;i++) {
            var Id=list[i].id;
            if(Id.indexOf(value)!= -1) {
                mini.alert('该账号已存在，不能重复添加', '警告');
                me.employeeSelect.setNameValue('','');
                return;
            }
        }
        if(me.showTaskType) {
            if(mini.isEmpty(me.taskTypeSelect.getValue()))
            {
                mini.alert('请选择负责人类型！', '警告');
                return;
            }
        }
        this.addItem(name,value);
        me.employeeSelect.setNameValue('','');        
    },
    //执行添加
    addItem:function(name,value){
        var me = this;
        if(me.showTaskType){
            var ty = this.taskTypeSelect.getText();
            if(ty=='DFC'){
                if(me.employeeTypes.getValue().indexOf(me.DFC)>-1) {
                    mini.alert("负责人只能存在一个DFC流程负责人");
                    return;
                }    
                name=name.indexOf('(')>-1?name:name+'(DFC)';
            }else if(ty=='MPM'){
                name =name.indexOf('(')>-1?name:name+ '(MPM)';
            }
        }
        me.listbox.addItem({id:value,
                         name:name                        
                        });
        if(me.showTaskType)
            this.modifyTypeValue();
        if(me.showUnit)
            this.modifyUnitValue();
        this.modifyValue();
        
    },
    //编辑
    onEdit:function() {
        var me=this;
        var selecteds = me.listbox.getSelecteds();
        if(selecteds.length==0){
             mini.alert('请选择需要编辑的员工账号！', '警告');
             return ;
        }
        var value = me.employeeSelect.getValue();
        if(mini.isEmpty(value)) {
            return;
        }
        if(selecteds.length>1){
             mini.alert('只能选择一条需要编辑的员工账号！', '警告');
             return ;
        }
        if(selecteds[0].id!=value){
            var list=me.listbox.getData();
            for(var i=0;i<list.length;i++){
                var Id=list[i].id;
                if(Id.indexOf(value)!= -1){
                    mini.alert('该账号已存在，不能重复添加', '警告');
                    me.employeeSelect.setNameValue('','');
                    return;
                }
            }
        }
        if(me.onDelete()){
            me.onAdd();
        }
    },
    //执行删除
    onDelete:function(){
    	var me = this;
        var selecteds = me.listbox.getSelecteds();
        if(mini.isEmpty(selecteds)){
             mini.alert('请选择负责人选项！', '警告');
             return false;
        }
        if(selecteds.length==0){
             mini.alert('请选择需要清除的员工账号！', '警告');
             return ;
        }
        me.listbox.removeItems(selecteds);
        if(me.showTaskType){
            this.modifyTypeValue();
        } 
        this.modifyValue();
        this.employeeSelect.setNameValue('','');
        return true;
    },
    setDefaultValue:function(attributes){
        if(!attributes) return;
        var me = this;
        var params = attributes.split(';');
        jQuery(params).each(function(index,param){
            try{
                var field = param.split(':')[0].trim();
                var value = param.split(':')[1].trim();
                if(field=='fieldName')
                    me.fieldName.setText(value+":");
                else if(field=='showTaskType'){
                    me.setShowTaskType(value=="true");
                }else if(field=='showUnit'){
                    me.setUnit(value=="true"?true:false);
                }if(field=='showUnitValue'){
                    me.showUnitValue=(value=="true")?true:false;
                }
            }catch(ex){}
        });
    },
    setSecretCheck:function(value) {
        this.secretCheck=value;
    },    
    modifyTypeValue:function(){
        var me = this;
        var types = [];
        jQuery(me.listbox.getData()).each(function(index,record){
            var name=record.name;
            name=name.substring(name.indexOf('(')+1,name.length-1);
            var typeId=name=='DFC'?me.DFC:me.MPM; 
             types.push(record.id+'@'+typeId);
        });
        me.employeeTypes.setValue(types.toString());
        me.fire("modifyTypeValue",types.toString());
    },
    modifyUnitValue:function(){
        var me = this;
        var types = [];
        jQuery(me.listbox.getData()).each(function(index,record){
            var name=record.name;
            name=name.substring(name.indexOf('[')+1,name.indexOf(']')).replace('%','');
             types.push((record.id.indexOf('@')>=0?record.id.substring(0,record.id.indexOf('@')):record.id)+'@'+name);
        });
        var fireType=this.isMaster?"modifyMasterUnitValue":"modifyPrincipalUnitValue";
        me.fire(fireType,types.toString());
    },
    modifyValue:function(){
        var me = this;
        var ids = [];
        jQuery(me.listbox.getData()).each(function(index,record){
            ids.push(record.id);
        });
        me.value = ids.toString();
    },
    setShowTaskType:function(value){
        this.showTaskType=value;
        if(value){
            this.taskTypeSelect.show();
        } else this.taskTypeSelect.hide();
    },
    setFieldName:function(fName){
        this.isMaster=fName.indexOf('主管')>=0?true:false;
    },
    setUnit:function(value){
        this.showUnit=value;
        this.showUnitValue=value;
        if(value){
            this.unit.show();
        } else{
            this.unit.hide();
        }
    },
    hideField: function () {
        this.fieldName.hide();
    },
    setAllDisabled:function(){
        var cs=mini.getChildControls(this);
        for(var i=0;i<cs.length;i++) {
            var c=cs[i];
            c.setEnabled(false);
        }
    },
    getListData:function(){
       return this.listbox.getData();
    },
    getItemSet:function(){
        var data = {id:'',name:''};
        var items = this.listbox.getData();
        if(items.length > 0){
            for(var i=0;i<items.length;i++){
                var d = items[i];
                data.id += ',' + d.id;
                data.name += ',' + d.name;
            };
            data.id = data.id.substring(1);
            data.name = data.name.substring(1);
        }
       return data;
    },
    getValue:function(){
        var ids = [];
        var items = this.listbox.getData();
        jQuery(items).each(function(index,record){
            ids.push(record.id);
        });
        return ids.toString();
    }
});
mini.regClass(Gnc.EmployeeListBox, "Gnc.EmployeeListBox");